package com.example.blockingqueue.blockingqueueconfig.delayqueue;

import com.example.blockingqueue.controller.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.DelayQueue;

/*ApplicationRunner的作用--项目启动后就执行run方法*/
@Component
public class DelayQueueConsumer implements ApplicationRunner {

    @Autowired
    private DelayQueue<Order> delayQueue;

    @Override
    public void run(ApplicationArguments args) {
        /*开启一个线程 去监听 delayQueue*/
        new Thread(() -> {
            while (true) {
                try {
                    Order order = delayQueue.take();
                    order.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
